var mpsurl = "https://test-hrms.pa18.com:9444";
//var mpsurl ="https://hrms.pa18.com";
function leftmenu(leftmenu,wrap){
//$(leftmenu).hide();
$(leftmenu).html(
"<section class=\"left-side-content\">"
+"<dl class=\"side-content home\">"
+"<dt class=\"side-content-title\">"
+"<div class=\"row-webkit-box\" id=\"index\">"
+"<div class=\"row-left-icon\"><i></i></div>"
+"<div class=\"info-list-cont\">首页</div> "
+"</div>"
+"</dt>"
+"</dl>"
+"<div id=\"J-SideMenu\">"
+"<div>"
+"<dl class=\"side-content kq swtich-menu curr-show\">"
+"<dt class=\"side-content-title\">"
+"<div class=\"row-webkit-box\">"
+"<div class=\"row-left-icon\"><i></i></div>"
+"<div class=\"info-list-cont\">考勤</div>"
+"<div class=\"info-list-icon\"><i></i></div>"
+"</div>"
+"</dt>"
+"<dd class=\"side-content-cont row-info single-row\">"
+"<ul class=\"row-info-list icon-click\">"
+"<li>"
+"<div class=\"row-webkit-box\" onclick=\"gotoUrl('/mobile/mywork/ycwork.html');\">"
+"<div class=\"row-left-icon\"></div>"
+"<div class=\"info-list-cont\">查异常</div>"
+"<div class=\"info-list-icon next-icon\"></div>"
+"</div>"
+"</li>"
+"<li>"
+"<div class=\"row-webkit-box\" onclick=\"gotoUrl('/mobile/mywork/sqwork.html');\">"
+"<div class=\"row-left-icon\"></div>"
+"<div class=\"info-list-cont\">去请假</div>"
+"<div class=\"info-list-icon next-icon\"></div>"
+"</div>"
+"</li>"
+"<li>"
+"<div class=\"row-webkit-box\" onclick=\"gotoUrl('/mobile/mywork/qjwork.html');\">"
+"<div class=\"row-left-icon\"></div>"
+"<div class=\"info-list-cont\">查假单</div>"
+"<div class=\"info-list-icon next-icon\"></div>"
+"</div>"
+"</li>"
+"</ul>"
+"</dd>"
+"</dl>"
+"<dl class=\"side-content ydbg swtich-menu curr-show\">"
+"<dt class=\"side-content-title\">"
+"<div class=\"row-webkit-box\">"
+"<div class=\"row-left-icon\"><i></i></div>"
+"<div class=\"info-list-cont\">月度绩效</div>"
+"<div class=\"info-list-icon\"><i></i></div>"
+"</div>"
+"</dt>"
+"<dd class=\"side-content-cont row-info single-row\">"
+"<ul class=\"row-info-list icon-click\">"
+"<li>"
+"<div class=\"row-webkit-box\" onclick=\"toReport('');\">"
+"<div class=\"row-left-icon\"></div>"
+"<div class=\"info-list-cont\">写汇报</div>"
+"<div class=\"info-list-icon next-icon\"></div>"
+"</div>"
+"</li>"
+"<li>"
+"<div class=\"row-webkit-box\" onclick=\"gotoUrl('/mobile/underlingReport/xslb.html');\">"
+"<div class=\"row-left-icon\"></div>"
+"<div class=\"info-list-cont\">审汇报</div>"
+"<div class=\"info-list-icon next-icon\"></div>"
+"</div>"
+"</li>"
+"</ul>"
+"</dd>"
+"</dl>"
+"<dl class=\"side-content cxs\">            "
+"<dt class=\"side-content-title\">"
+"<div class=\"row-webkit-box icon-click\" onclick=\"gotoUrl('/mobile/mysalary/gzd-chaxun.html');\">"
+"<div class=\"row-left-icon\"><i></i></div>"
+"<div class=\"info-list-cont\">查薪水</div>"
+"<div class=\"info-list-icon next-icon\"></div>"
+"</div>"
+"</dt>"
+"</dl>"
+"<dl class=\"side-content yw\">            "
+"<dt class=\"side-content-title\">"
+"<div class=\"row-webkit-box icon-click\" onclick=\"gotoUrl('/mobile/yzx/yzx.html');\">"
+"<div class=\"row-left-icon\"><i></i></div>"
+"<div class=\"info-list-cont\">易问（试用版）</div>"
+"<div class=\"info-list-icon next-icon\"></div>"
+"</div>"
+"</dt>"
+"</dl>"
+"<dl class=\"side-content mszq\">"
+"<dt class=\"side-content-title\">"
+"<div class=\"row-webkit-box icon-click\"  onclick=\"gotoUrl('/mobile/myfund/mszq.html');\">"
+"<div class=\"row-left-icon\"><i></i></div>"
+"<div class=\"info-list-cont\">易赚钱</div>"
+"<div class=\"info-list-icon next-icon\"></div>"
+"</div>"
+"</dt>"
+"</dl>"
+"<dl class=\"side-content set\">"
+"<dt class=\"side-content-title\">"
+"<div class=\"row-webkit-box icon-click\" onclick=\"gotoUrl('/mobile/sz.html');\">"
+"<div class=\"row-left-icon\"><i></i></div>"
+"<div class=\"info-list-cont\">设置</div>"
+"<div class=\"info-list-icon next-icon\"></div>"
+"</div>"
+"</dt>"
+"</dl>"
+"<dl class=\"side-content yjfk\">"
+"<dt class=\"side-content-title\">"
+"<div class=\"row-webkit-box icon-click\" onclick=\"gotoUrl('/mobile/yjfk.html');\">"
+"<div class=\"row-left-icon\"><i></i></div>"
+"<div class=\"info-list-cont\">意见反馈</div>"
+"<div class=\"info-list-icon next-icon\"></div>"
+"</div>"
+"</dt>"
+"</dl>"
+"</div>"
+"</div>"
+"</section>");
	var sideMenu = $("#J-SideMenu");
	var btnMenu = $("#J-BtnMenu");
	var wrap = $(wrap);
	sideMenu.find(".swtich-menu > dt").on("tap", function(){
	    var parentElem = $(this).parent();
	    parentElem.hasClass("curr-show") ? parentElem.removeClass("curr-show") :  parentElem.addClass("curr-show") ;
	    countPageHeight();
	    mIscroll.refresh();
	})

	btnMenu.on("tap", function(){
		var ua = navigator.userAgent;
		if(ua.indexOf("iPhone")>=0){
		}else{
			window.pa_app.closeKeyboard();
		}
		$("input,textarea").blur();
		setTimeout(function(){
			if(!wrap.hasClass("open-side")){
				//$(leftmenu).show();
				wrap.addClass("open-side");
			}
			else{
				wrap.removeClass("open-side");
				/*setTimeout(function(){
					$(leftmenu).hide();
				},500);
				*/
			}
			countPageHeight();
			mIscroll.refresh();
		},200);
	});
	wrap.bind("swipeLeft",function(){
		wrap.removeClass("open-side");
		/*setTimeout(function(){
			$(leftmenu).hide();
		},500);
		*/
	});
	wrap.bind("swipeRight",function(){
		//$(leftmenu).show();
		var ua = navigator.userAgent;
		if(ua.indexOf("iPhone")>=0){
		}else{
			window.pa_app.closeKeyboard();
		}
		$("textarea").blur();
		setTimeout(function(){
			wrap.addClass("open-side");
			countPageHeight();
			mIscroll.refresh();
		},200);
	});
	$("#index").tap(function(){
		setTimeout(function(){
			//location.href = '/mobile/index.html';
			gotoLocation("/mobile/index.html","");
		},500);
		//location.href = '/mobile/index.html';
	});
}
//ajax返回错误信息，弹出层
function errorMask(message){
	$("#message").html(message);
	$("#errorMask").show();
}
function gotoUrl(url){
	//location.href=url;
	gotoLocation(url,"");
}
//页面跳转
function gotoLocation(url,param){
	var ua = navigator.userAgent;
	if(ua.indexOf("iPhone")>=0){
		if(param==""){
			location.href=url;
		}else{
			location.href=url+"?"+param;
		}
	}else{
		window.pa_app.location(url,param);
	}
}
//解析url参数
function getQueryParam (name){
	//参数：变量名，url为空则表从当前页面的url中取
	var u;
	var ua = navigator.userAgent;
	if(ua.indexOf("iPhone")>=0){
		u = window.location.search.replace("&amp;", "&");
	}else{
		u = window.pa_app.GetParam();
	}
	//var u  = arguments[1] || window.location.search.replace("&amp;", "&"),
	var	reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"),
		r = u.substr(u.indexOf("\?")+1).match(reg);
	return r!=null?r[2]:"";
}
var gurl="";
//根据返回码判断是否跳转到登陆页或验证验证码
function checkLogin(resCode,tourl,message){
	gurl = tourl;
	if(resCode=="500702"){
		//session过期
		var token="";
		//读取token
		var ua = navigator.userAgent;
		if(ua.indexOf("iPhone")>=0){
			//location.href="/mobile/login.html";
			window.location="/objc://read";
		}else{
			token = window.pa_app.GetToken();
			getToken(token);
		}
	}else if(resCode=="500802"){
		//opt
		//location.href="/mobile/user_otpyz.html?tourl="+tourl;
		var param = "tourl="+tourl;
		gotoLocation("/mobile/user_otpyz.html",param);
	}else if(resCode=="500803"){
		//um opt
		var param = "tourl="+tourl;
		gotoLocation("/mobile/user-yz.html",param);
		//location.href="/mobile/user-yz.html?tourl="+tourl;
	}else{
		//errorMask(message);
		setPromptForInput(message);
	}
}

//先检测汇报状态,再判断调整页面
function toReport(report_date){
	if(report_date==""){
		var date = new Date();
		var month = date.getMonth()+1;
		var day = date.getDate();
		var year=date.getFullYear();
		if(day<10){
			if(month==1){
				month=12;
				year = year-1;
			}else
				month=month-1;
		}
		if(month<10)
			month="0"+month;
		report_date = year+"/"+month;
	}
	$.ajax({
		type: "post",
		url: mpsurl+"/mobile/report/queryMyReport.do",
		dataType: "json",
		data:{
			report_date:report_date
		},
		success: function (data) { 
			var obj = eval(data);
			var resultCode = obj.resultCode;
			var resultMessage = obj.resultMessage;
			checkLogin(obj.resultCode);
			if(resultCode=="500600"){
				var report = obj.value.report;
				var status =  report.pAIC_MON_STATUS;
				if(status=="USUB" || status=="BAC" || status=="NON"){
			    	//location.href="/mobile/kpi/kpi-todo.html?report_date="+report_date;
			    	var param = "report_date="+ report_date;
					gotoLocation("/mobile/kpi/kpi-todo.html",param);
			    }else{
			    	//location.href="/mobile/myreport/wdydbg-chakan.html?report_date="+report_date;
			    	var param = "report_date="+ report_date;
					gotoLocation("/mobile/myreport/wdydbg-chakan.html",param);
			    }
//			}else if(resultCode=="500611"){
//				//请先制定年度计划
//				location.href="/mobile/myreport/wdydbg-chakan.html?report_date="+report_date;
			}else{
				//location.href="/mobile/myreport/wdydbg-chakan.html?report_date="+report_date;
				var param = "report_date="+ report_date;
				gotoLocation("/mobile/myreport/wdydbg-chakan.html",param);
			}
		}
	});
}

function getToken(token){
	//免登录
	if(gurl==""){
		gurl="/mobile/index.html";
	}
	if(token==""||token.length<8){
		var ua = navigator.userAgent;
		if(ua.indexOf("iPhone")>=0){
			window.location="/objc://view";
		}
	}else{
		$.ajax({
			type:"post",
			url:mpsurl+"/mobile/register/loginToKen.do",
			data:{token:token},
			dataType:"text",
			timeout:120000,
			success:function(data){
				data = eval("("+data+")");
				if(data.resultCode=="500600"){
					var ua = navigator.userAgent;
					if(ua.indexOf("iPhone")>=0){
						window.location="/objc://view";
					}
					location.href=gurl;
				}else{
					var ua = navigator.userAgent;
					if(ua.indexOf("iPhone")>=0){
						//window.location="/objc://write&token=''";
						window.location = "/objc://quit";
					}else{
				    	window.pa_app.SetToken("");
						gotoLocation("/mobile/login.html","");
				    }
					//location.href="/mobile/login.html";
					//closePrompt(data.resultCode);
				}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				//closePrompt("网络异常");
				var ua = navigator.userAgent;
					if(ua.indexOf("iPhone")>=0){
						//window.location="/objc://write&token=''";
						window.location = "/objc://quit";
					}else{
				    	window.pa_app.SetToken("");
						gotoLocation("/mobile/login.html","");
				    }
					//location.href="/mobile/login.html";
			}
		});
	}
}

/*
 * 页面数据校验提示框
 */
function setPromptForInput(msg){
	var obj = document.getElementById("validateBox2");
	if(null == obj){
		var promptBox = 
			"<div id='validateBox2' style='position: fixed;left: 20%;bottom: 15%;width: 60%;z-index: 1000;'>"+
			"<div style='position: absolute;width: 100%;height: 100%;background: #000;-webkit-border-radius: 8px;border-radius: 8px;opacity: .6;z-index: 1;'>"+
			"</div>"+
			"<div style='position: relative;display: -webkit-box;color: #fff;min-height:20px;-webkit-box-orient: horizontal;-webkit-box-pack: center;padding:5px 8px;font-size: 28px;text-align: center;z-index: 2;'>"+
			msg+"</div>"+
			"</div>";
		$("body").append(promptBox);
		// 定时关闭提示框
		setTimeout(function(){
			$("#validateBox2").remove("slow");
		},2000);
	}
}

//计算页面高度创建iScroll
function countPageHeight(){  
	$("#J-SideMenu").css({"position": "absolute", "width": "100%", "height": "100%", "overflow":"hidden"});
	winHeight = $(window).height();
	$("#wrap").height(winHeight);
	$("#leftmenu").height(winHeight);
	homeHeight = $("#J-SideMenu").prev("dl").height(); 
	$("#J-SideMenu").height(winHeight - homeHeight);
}

//创建滚动条
function CreateIscroll(){ 
	countPageHeight();
	mIscroll = new iScroll("J-SideMenu", {bounce:false});
}
 
//页面加载完后执行js代码
var $$ = function(func){
    var oldOnload =window.onload;
    if(typeof window.onload != 'function'){
        window.onload = func;
    }else{
        window.onload = function(){
            oldOnload();
            func();
        }
    }
}  

//在所有js执行后调用这里代码
$$(function(){   
	CreateIscroll();
});
